package com.simtop.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.*;
import com.simtop.pojo.wddata.WdDate;
import com.simtop.service.ApiService;
import com.simtop.service.UserService;
import com.simtop.util.JwtUtil;
import com.simtop.util.ReadExcelUtil;
import com.simtop.util.ReadExcelUtil2;
import com.simtop.util.SHA256Util;
import com.simtop.vo.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

import com.simtop.util.excel.ExcelUtils;


@RestController
@RequestMapping("/api")
@CrossOrigin
public class ApiController {

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private ApiService apiService;

    @Autowired
    private UserService userService;

    @PostMapping("/UpswfileOne")
    @ResponseBody
    public ServerResponse UpswfileOne(HttpServletRequest request,MultipartFile file) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            //情况表
            apiService.deletesw();
            apiService.deletesws();
            //String filepath = "C:\\webProject\\Java\\yy\\泳道01.xlsx";
             String filepath = "C:\\Users\\Administrator\\Desktop\\yy\\泳道01.xlsx";
            //String  filepath = "F:\\" + userVo.getLoginName() + "\\用户上传.csv";


            file.transferTo(new File(filepath));


        return ServerResponse.createBySuccess("泳道01.xlsx");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }


    }


    @PostMapping("/UpswfileOneUser")
    @ResponseBody
    public ServerResponse UpswfileOneUser(HttpServletRequest request,MultipartFile file) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            //String filepath = "C:\\webProject\\Java\\yys\\user.xlsx";
            String filepath = "C:\\Users\\Administrator\\Desktop\\yys\\user.xlsx";
            //String  filepath = "F:\\" + userVo.getLoginName() + "\\用户上传.csv";


            file.transferTo(new File(filepath));

            String path = "user.xlsx";

            String as = "C:\\webProject\\Java\\yys\\";

            List<userPo> userPos = ReadExcelUtil2.readExcel(as, path, "Sheet1");
            for (int i = 0; i < userPos.size(); i++) {
                System.out.println(JSON.toJSONString(userPos.get(i)));
            }
            List<UserVoS> userVoSArrayList = new ArrayList<>();
            for (userPo os:userPos
            ) {
                String userteam = os.getUserteam();
                int teamid = apiService.findTameid(userteam);

                UserVoS userVoS = new UserVoS();
                userVoS.setUserId(Integer.valueOf(os.getUserid()));
                userVoS.setUserName(os.getUsername());
                userVoS.setLoginIp("127.0.0.1");
                userVoS.setLoginName(os.getUsername());
                userVoS.setRoleId(5);
                userVoS.setLoginCount(1);
                userVoS.setLoginTime(1);
                userVoS.setLastTime(new Date());
                userVoS.setLoginStatus(0);
                userVoS.setTameId(teamid);
                userVoS.setUserImage("http://82.156.232.217/swimage/login.jpg");
                userVoS.setPassword(SHA256Util.generateShaPwd("123456"));
                userVoSArrayList.add(userVoS);


            }
            apiService.saveAlluser(userVoSArrayList);

            return ServerResponse.createBySuccess();

        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }


    }




    @PostMapping("/swfileOne")
    @ResponseBody
    public ServerResponse swfile(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        Integer coach=u.getId();
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }

        try {

            JSONObject jsonObject = JSON.parseObject(data);
            String path = jsonObject.getString("path");
            //String as = "C:\\Users\\Administrator\\Desktop\\yy\\";

            String as = "C:\\webProject\\Java\\yy\\";
            String swtype = jsonObject.getString("swtype");
            Integer swstatus = 1;
            //String dir = "C:\\Users\\Administrator\\Desktop\\";
            List<YongdaoPo> YongDao = ReadExcelUtil.readExcel(as, path, "Sheet1");
            for (int i = 0; i < YongDao.size(); i++) {
                System.out.println(JSON.toJSONString(YongDao.get(i)));
            }
            ArrayList<Object> objects = new ArrayList<>();
            for (YongdaoPo os:YongDao
                 ) {
                os.setSwtype(swtype);
                os.setSwstatus(swstatus);
                os.setCoach(coach);
                System.out.println(swstatus);
                System.out.println(coach+"教练");
            }
            apiService.insertYongdao(YongDao);

            //查询所有的数据



            List<SwyDone> sa = apiService.findAllSwyDone();

            return ServerResponse.createBySuccess("获取内容成功",sa);








        } catch (Exception e) {
            e.printStackTrace();
        }



        return ServerResponse.createBySuccess();
    }


    @PostMapping("/deletestatusByid")
    @ResponseBody
    public ServerResponse deletestatusByid(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }

        try {

            JSONObject jsonObject = JSON.parseObject(data);
            Integer swuserid = jsonObject.getInteger("swuserid");

            apiService.deletestatusByid(swuserid);



            return ServerResponse.createBySuccess();








        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }

    }



    @PostMapping("/swfileOnes")
    @ResponseBody
    public ServerResponse swfileOnes(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }

        try {


            List<SwyDone> sa = apiService.findAllSwyDone();

            return ServerResponse.createBySuccess("获取内容成功",sa);








        } catch (Exception e) {
            e.printStackTrace();
        }



        return ServerResponse.createBySuccess();
    }


    @PostMapping("/deletesw")
    @ResponseBody
    public ServerResponse deletesw(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }

        try {



            apiService.deletesw();
            apiService.deletesws();
            apiService.changeStatus1();
            apiService.changeStatus2();

             return ServerResponse.createBySuccess();



        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }



    }

    @PostMapping("/datalist")
    @ResponseBody
    public ServerResponse datalist(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }

        try {
            JSONObject jsonObject = JSON.parseObject(data);
            Integer swuserid = jsonObject.getInteger("swuserid");
            newOneListVo newOneListVo =  apiService.findOneFx(swuserid);



            return ServerResponse.createBySuccess(newOneListVo);



        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }



    }


    @PostMapping("/rustDate")

    public zxxVo rustDate(){



        List<SwyDone2> sa = apiService.findAllSwyDone2();
        //查询表中状态
        int status = apiService.findAllSwyDoneOne();
        zxxVo zxxVoS = new zxxVo();
        zxxVoS.setStatus(status);
        zxxVoS.setSas(sa);


        return zxxVoS;


    }

    @PostMapping("/rustDate2")

    public ServerResponse rustDate2(){
try {
    Thread.sleep(1000);
}catch (Exception e){
    e.printStackTrace();
}

        apiService.deletesw();
        apiService.deletesws();
        //改变状态为 0
        apiService.changeOneStatus();

        apiService.changeOneStatus2s();

        //清空表 list



        return ServerResponse.createBySuccess();


    }

    @PostMapping("/findnewstatus")

    public ServerResponse findnewstatus(){

        int status = apiService.finStatuszt();
        swstatus swstatus = new swstatus();
        swstatus.setStatus(status);


        return ServerResponse.createBySuccess(swstatus);


    }

    @PostMapping("/startexe")

    public ServerResponse startexe(){

        {
            Runtime rn = Runtime.getRuntime();
            Process  p= null;
            try {
                //执行的文件的位置
                String cmd = "E:\\exe\\exe\\PoseAnalysisSystem.exe";
                p = rn.exec(cmd);

                return ServerResponse.createBySuccess();

            } catch (Exception e) {
                return ServerResponse.createByError();

            }
        }




    }

    @PostMapping("/stopexe")

    public ServerResponse stopexe(){

        String cmd = "taskkill /f /t /im "+"PoseAnalysisSystem.exe";
        try
        {
            //exec执行cmd命令
            Process process = Runtime.getRuntime().exec(cmd);
            //获取CMD命令结果的输出流
            InputStream fis=process.getInputStream();
            InputStreamReader isr=new InputStreamReader(fis,"GBK");
            //使用缓冲器读取
            BufferedReader br=new BufferedReader(isr);
            String line=null;
            //全部读取完成为止，一行一行读取
            while((line=br.readLine())!=null)
            {
                //输出读取的内容
                System.out.println( line );
            }
            return ServerResponse.createBySuccess();
        }catch (IOException e)
        {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }






    @PostMapping("/sendsize")
    public ServerResponse sendsize(@RequestBody String data ,HttpServletRequest  request , HttpServletResponse response){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        Integer  coach=u.getId();
        apiService.deletesw();


        List<SwyDone> yongdaoPoList = JSON.parseArray(data, SwyDone.class);

        for (SwyDone as:yongdaoPoList
             ) {
            as.setSwStatus("1");
            as.setCoach(coach);
            System.out.println(as.getSwuserid());
            System.out.println(as.getSwid());

        }
        apiService.saveAllcxList(yongdaoPoList);

        //拿到最小的id 给一个初始时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatbegin = simpleDateFormat.format(new Date());
        apiService.saveBegin(formatbegin);



        return ServerResponse.createBySuccess();


    }



    @PostMapping("/rustswmming")

    public ServerResponse rustswmming(@RequestBody String data){
        //取到学号入库

        JSONObject jsonObject = JSON.parseObject(data);
/*        String name = jsonObject.getString("name");
        String team = jsonObject.getString("team");
        Integer userid = jsonObject.getInteger("userid");*/
        //根据 stu_id 改变状态

        System.out.println(data);
        String ids = jsonObject.getString("stu_id");
        Integer swuserid = Integer.valueOf(ids);
        Integer last_stu = jsonObject.getInteger("last_stu");
        Integer score= jsonObject.getInteger("score");
        //根据学号 保存 成绩
        apiService.changeScore(score,swuserid);


/*        if(last_stu  != 1){
            //改变他的下一位id
            int id = apiService.finids(swuserid);
            int lastid = id + 1 ;
            apiService.changezbStatus(lastid);

        }*/

        apiService.changeOne(swuserid);

        apiService.insertfxuser(swuserid,data);

        //拿到结束的时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatend = simpleDateFormat.format(new Date());
        apiService.saveEnd(formatend,swuserid);

        //给下一个人开始时间
        //获取当前学生的id
        int id = apiService.getid(swuserid);

        int last = id + 1;
        apiService.getStartTime(last,formatend);

        if(last_stu  == 1){
            //代表最后一位
            apiService.changeOneStatus2();

            //将所有的 表 存入到 成绩表
            //拿到 所有的list 然后 加入
            List<Swread> swreadList = apiService.swreadList();
            for (Swread as:swreadList
                 ) {
                Date endTime = as.getEndTime();
                long timeend = endTime.getTime();
                Date beginTime = as.getBeginTime();
                long timebegin = beginTime.getTime();
                long lasts = timeend - timebegin;
                long lastst = lasts / 1000;
                Integer coach=as.getCoach();
                as.setUseTime((int) lastst);
                as.setCreateTime(new Date());
            }
            //加入
            apiService.asveAllread(swreadList);

        }

        return ServerResponse.createBySuccess("成功");
    }

    @PostMapping("/findswList")
    public ServerResponse findswList(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if(StringUtils.isEmpty(pageNum) || pageNum == 0){
                pageNum = 1;
            }
            if(StringUtils.isEmpty(pageSize) || pageSize == 0){
                pageSize = 10;
            }
            PageHelper.startPage(pageNum,pageSize);
            String tameName = jsonObject.getString("tameName");
            String types = jsonObject.getString("types");
            Date readTimeBegin = jsonObject.getDate("beginTime");
            Date readTimeEnd = jsonObject.getDate("endTime");
            String column = jsonObject.getString("column");
            String order = jsonObject.getString("order");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String readTimeBegins = null;
            String readTimeEnds = null;
            if(readTimeBegin != null && readTimeEnd != null) {

                readTimeBegins = df.format(readTimeBegin);
                readTimeEnds = df.format(readTimeEnd);
                List<Swread> swreadList = apiService.findsw(tameName,types,readTimeBegins,readTimeEnds,column,order);
                PageInfo<Swread> pageInfo = new PageInfo<Swread>(swreadList);
                return ServerResponse.createBySuccess("获取内容成功",pageInfo);
            }

            List<Swread> swreadList = apiService.findsw(tameName,types,readTimeBegins,readTimeEnds,column,order);
            PageInfo<Swread> pageInfo = new PageInfo<Swread>(swreadList);
            return ServerResponse.createBySuccess("获取内容成功",pageInfo);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


    @RequestMapping(value = "saveswList",method = RequestMethod.GET)
    public ServerResponse saveswList(String id, HttpServletRequest  request , HttpServletResponse response) {



        try {

            if(id == null){
                try {
                    //     System.out.println("西夏哈哈");
                    //找到所有的学生
                //    List<Swread> findAlldeviceListAllBystatus= apiService.findAlldeviceListAllBystatus2();
                    List<Swread> findSwread = apiService.findSwread();


                    ArrayList<MonitoringDataExcelVo> excelVos = new ArrayList<MonitoringDataExcelVo>();
                    for (Swread as:findSwread
                    ) {


                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        MonitoringDataExcelVo monitoringDataExcelVo = new MonitoringDataExcelVo();
                        monitoringDataExcelVo.setId(as.getId());
                        monitoringDataExcelVo.setUser(as.getUser());
                        monitoringDataExcelVo.setUserId(as.getUserId());
                        monitoringDataExcelVo.setBeginTime(sdf.format(as.getBeginTime()));
                        monitoringDataExcelVo.setEndTime(sdf.format(as.getEndTime()));
                        monitoringDataExcelVo.setCj(as.getCj());
                        monitoringDataExcelVo.setTypes(as.getTypes());
                        monitoringDataExcelVo.setUserTime(as.getUseTime() + "秒");
                        monitoringDataExcelVo.setTeam(as.getTeam());
                        excelVos.add(monitoringDataExcelVo);

                    }


                    ExportParams exportParams = new ExportParams("泳道训练记录全部数据", "泳道训练记录全部数据");//设置sheet
                    Workbook workbook = ExcelExportUtil.exportExcel(exportParams, MonitoringDataExcelVo.class, excelVos);//问好处填vo
                    //String fileName = new String("社群全部成绩数据".getBytes(), "UTF-8") + ".xls";//设置excel的名称后缀
                    //response.setContentType("application/vnd.ms-excel;charset=UTF-8");//类型
                    //response.setHeader("Content-Disposition", "attachment;filename=" + fileName);//设置响应头
                    //response.setCharacterEncoding("UTF-8");//格式
                    String exportFileName ="泳道训练记录全部数据.xlsx";
                    String fileNameURL = URLEncoder.encode(exportFileName, "UTF-8");
                    response.setHeader("Content-disposition", "attachment;filename="+fileNameURL+";"+"filename*=utf-8''"+fileNameURL);

                    //response.setContentType("application/vnd.ms-excel;charset=UTF-8");//类型
                    response.setContentType("application/vnd.ms-excel");
                    OutputStream os = response.getOutputStream();

                    workbook.write(os);//写入
                    os.flush();


                    try {
                        os.close();//关闭
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return ServerResponse.createBySuccess();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return ServerResponse.createByError();
                }



            }



            String[] split = id.split(",");

            ArrayList<Integer> objects = new ArrayList<>();
            Arrays.stream(split).iterator();



            ArrayList<Integer> sqsyidlist = new ArrayList<>();


            Iterator<String> iterator = Arrays.stream(split).iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                Integer i = Integer.valueOf(next);
                sqsyidlist.add(i);

                // apiService.finOne
            }




            List<Swread> findSwread2= apiService.findSwread2(sqsyidlist);


            ArrayList<MonitoringDataExcelVo> excelVos = new ArrayList<MonitoringDataExcelVo>();
            for (Swread as:findSwread2
            ) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                MonitoringDataExcelVo monitoringDataExcelVo = new MonitoringDataExcelVo();
                monitoringDataExcelVo.setId(as.getId());
                monitoringDataExcelVo.setUser(as.getUser());
                monitoringDataExcelVo.setUserId(as.getUserId());
                monitoringDataExcelVo.setBeginTime(sdf.format(as.getBeginTime()));
                monitoringDataExcelVo.setEndTime(sdf.format(as.getEndTime()));
                monitoringDataExcelVo.setCj(as.getCj());
                monitoringDataExcelVo.setTypes(as.getTypes());
                monitoringDataExcelVo.setUserTime(as.getUseTime() + "秒");
                monitoringDataExcelVo.setTeam(as.getTeam());
                excelVos.add(monitoringDataExcelVo);

            }


            ExportParams exportParams = new ExportParams("泳道训练记录数据", "泳道训练记录数据");//设置sheet
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, MonitoringDataExcelVo.class, excelVos);//问好处填vo
            //String fileName = new String("社群成绩".getBytes(), "ISO8859-1") + ".xls";//设置excel的名称后缀
            //String fileNames = "社群用户成绩.xls";
            // fileNames= new String((fileNames).getBytes(), "UTF-8");
            String exportFileName ="泳道训练记录数据.xlsx";
            String fileNameURL = URLEncoder.encode(exportFileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+fileNameURL+";"+"filename*=utf-8''"+fileNameURL);
            //response.setContentType("application/vnd.ms-excel;charset=UTF-8");//类型
            response.setContentType("application/vnd.ms-excel");
            //response.setHeader("Content-Disposition", "attachment;filename=" + fileNames);//设置响应头
            //response.setCharacterEncoding("UTF-8");//格式
            OutputStream os = response.getOutputStream();

            workbook.write(os);//写入
            os.flush();


            try {
                os.close();//关闭
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ServerResponse.createBySuccess();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ServerResponse.createByError();
        }



    }




    @PostMapping("/deleteswList")
    @ResponseBody
    public ServerResponse deleteswList(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        String id = jsonObject.getString("id");
        String[] split = id.split(",");
        ArrayList<Integer> sqsyidlist = new ArrayList<>();


        Iterator<String> iterator = Arrays.stream(split).iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            Integer i = Integer.valueOf(next);
            sqsyidlist.add(i);

            // apiService.finOne
        }
        userService.deletefindAllListas(sqsyidlist);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/test")

    public void test(){
        List<Swread> swreadList = apiService.swreadList();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        for (Swread as:swreadList
        ) {
            Date endTime = as.getEndTime();
            long timeend = endTime.getTime();
            Date beginTime = as.getBeginTime();
            long timebegin = beginTime.getTime();
            long lasts = timeend - timebegin;
            long lastst = lasts / 1000;
            as.setUseTime((int) lastst);
            as.setCreateTime(new Date());
        }
        //加入
        apiService.asveAllread(swreadList);
    }

    public  static long tosend(long times){
        return  times / 1000;
    }


    @PostMapping("/newrustDate")

    public swstatus newrustDate(){
        int status = apiService.selectStatus();
        swstatus swstatuss = new swstatus();
        swstatuss.setStatus(status);


        return swstatuss;
    }

    @PostMapping("/rustDates")
    @ResponseBody
    public ServerResponse rustDates(){
        apiService.changeStatus1();
        apiService.changeStatus2();


        return ServerResponse.createBySuccess();
    }




    @PostMapping("/changedata")
    @ResponseBody
    public ServerResponse changedata(){


        List<SwyDone> sa = apiService.findAllSwyDone1();

        return ServerResponse.createBySuccess(sa);
    }


    @PostMapping("/changedata2")
    @ResponseBody
    public ServerResponse changedata2(){


        List<SwyDone> sa = apiService.findAllSwyDone1();

        return ServerResponse.createBySuccess(sa);
    }
    /*
    * 接收参数
    *
    * */
    @PostMapping("/rtdata")
    public void rtdata(@RequestBody String data, HttpServletRequest request) {
        /*
       //转换实体类
        System.out.println(data);
        JSONObject jsonObject = JSON.parseObject(data);
        String knd = jsonObject.getString("knd");
        String rts = jsonObject.getString("rts");
        List<WdDate> list = JSONObject.parseArray(rts, WdDate.class);
        System.out.println("测试转list==============开始");
        System.out.println(JSONObject.toJSONString(list));
        System.out.println("测试转list==============结束");
        System.out.println(knd);*/
        //获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String createtime = df.format(new Date());
        //当前厂房信息
        String plant = "A6";
        apiService.saverapi(data, createtime,plant);
        try {
            //拿到所有所有有用的信息放入数据处理表
            JSONObject jsonObject = JSON.parseObject(data);
            String rts = jsonObject.getString("rts");
            List<WdDate> lastDate  = JSON.parseArray(rts,WdDate.class);
            boolean managedate =  apiService.managedata(lastDate,plant);


            //System.out.println("传输正常" + createtime);
        }
        catch (Exception e){
            e.printStackTrace();
            //System.out.println("数据传输异常" + e);
        }
    }



    @PostMapping("/swCore")
    @ResponseBody
    public ServerResponse swCore(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        Echarts3Vo echarts3Vo = apiService.findScore();

        return ServerResponse.createBySuccess(echarts3Vo);
    }

    @PostMapping("/swCoreTime")
    @ResponseBody
    public ServerResponse swCoreTime(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        //找到时间time
        List<String>  findGroupTime = apiService.findGroupTime();
        ArrayList<String> timelist = new ArrayList<>();
        ArrayList<Integer> count = new ArrayList<>();
        for (String as:findGroupTime
             ) {
            timelist.add(as);
            Integer counts = apiService.dinOneCount(as);
            count.add(counts);

        }
        TimeVo timeVo = new TimeVo();
        timeVo.setTimes(timelist);
        timeVo.setCountList(count);

        return ServerResponse.createBySuccess(timeVo);
    }

    @PostMapping("/swCoreContent")
    @ResponseBody
    public ServerResponse swCoreContent(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        Echarts3Vo echarts3Vo = apiService.findContent();

        Map tj = new HashMap<>();
        tj.put("单人泳姿", echarts3Vo.getC1());
        tj.put("50米自由泳", echarts3Vo.getC2());
        tj.put("自定义时长计距" , echarts3Vo.getC3());
        tj.put("自定义距离计时" , echarts3Vo.getC4());

        return ServerResponse.createBySuccess(tj);
    }


    @PostMapping("/findmessage")
    @ResponseBody
    public ServerResponse findmessage(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
       MessageVo messageVo =  apiService.findmessage();


        return ServerResponse.createBySuccess(messageVo);
    }


    @PostMapping("/changemessage")
    @ResponseBody
    public ServerResponse changemessage(@RequestBody String data , HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSON.parseObject(data);
            Integer status = jsonObject.getInteger("swStatus");
            String swContent = jsonObject.getString("swContent");
            apiService.changemessage(status , swContent);

            return ServerResponse.createBySuccess();
        }
        catch (Exception e){
            return ServerResponse.createBySuccess();
        }
    }



}
